using AdminAuthority.Attributes;

using Abc.Application;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Threading.Tasks;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    [NonValidation]
    //[AuthorizePolicy]
    [AllowAnonymous]
    [AuthorityController(typeCode: "HtAdmin", controllerDescription: "菜单")]
    public class MenuController : HtAdminBaseController
    {
        private readonly ILogger<MenuController> _logger;
        private readonly IMenuService _menuService;
        private readonly IPermissionService _permissionService;

        public MenuController(ILogger<MenuController> logger
        , IMenuService menuService
        , IPermissionService permissionService)
        {
            _logger = logger;
            _menuService = menuService;
            _permissionService = permissionService;
        }

        [HttpGet]
        [AuthorityPermission(name: "菜单列表", actions: "Index,GetList")]
        public async Task<IActionResult> Index()
        {
            return View();
        }

        [HttpGet, HttpPost]
        public async Task<JsonResult> GetList(MenuQuery searchquery)
        {
            searchquery.Limit = 9999;
            var pagedlist = await _menuService.GetAllByPageToLayui(searchquery);
            return Json(pagedlist);
        }

        [AuthorityPermission(name: "菜单详情", actions: "Info")]
        public async Task<ActionResult> Info(int? id)
        {
            MenuOut entity = null;
            if (id.HasValue)
            {
                entity = await _menuService.Get(id.Value);
            }
            return View(entity ?? new MenuOut());
        }

        [HttpPost]
        [AuthorityPermission(name: "菜单编辑", actions: "Edit")]
        public async Task<JsonResult> Edit(MenuInput input)
        {
            return Json(await _menuService.InsertOrUpdate(input));
        }

        [HttpPost]
        [AuthorityPermission(name: "删除", actions: "Delete")]
        public async Task<ActionResult> Delete(int id)
        {
            return Json(await _menuService.Delete(id));
        }

        [HttpPost]
        [AuthorityPermission(name: "设置状态", actions: "SetState")]
        public async Task<JsonResult> SetState(int id, bool state, string type)
        {
            var resultdata = await _menuService.SetState(id, state, type);
            return Json(resultdata);
        }

        [AuthorityPermission(name: "获取菜单树", actions: "GetMenuTrees")]
        public async Task<JsonResult> GetMenuTrees()
        {
            return Json(await _menuService.GetDtree());
        }

        /// <summary>
        /// 选择菜单权限
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        [AuthorityPermission(name: "选择菜单权限", actions: "SelectPermission,GetPermissionDtree")]
        public async Task<IActionResult> SelectPermission(int menuid = 0)
        {
            var menu = await _menuService.Get(menuid);
            return View(menu);
        }

        /// <summary>
        /// 获取权限数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetPermissionDtree(int menuid, string typecode = "HtAdmin")
        {
            var permissionDtree = await _menuService.GetDtreeCheck(menuid, typecode);
            return Json(permissionDtree);
        }

        /// <summary>
        /// 设置菜单权限
        /// </summary>
        /// <param name="menuid"></param>
        /// <param name="permissionstrs"></param>
        /// <returns></returns>
        [AuthorityPermission(name: "设置菜单权限", actions: "SetMenuPermission")]
        public async Task<JsonResult> SetMenuPermission(int id, string permissionstrs)
        {
            return Json(await _menuService.SetMenuPermission(id, permissionstrs));
        }
    }
}